home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / ubiquity / bin / ubiquity
Encoding:
Text File  |  2007-04-12  |  6.6 KB  |  213 lines

  1. #!/usr/bin/python
  2.  
  3. '''
  4. Installer
  5.  
  6. This is a installer program for a Ubuntu or Metadistros Live system.
  7. This is the main program, but there are also a couple of libraries to
  8. help it to work, such as the frontend.
  9. The way it works is simple. It detects the frontend to use, then
  10. load the module for that frontend. After that, it makes some calls
  11. through the frontend in order to get the info necessary to install.
  12.  
  13. Once it has the info, partitioning, format, copy the distro to the disk
  14. and configure everything.
  15. '''
  16.  
  17. import sys
  18. import os
  19. import errno
  20. import fcntl
  21. import shutil
  22. import syslog
  23. import atexit
  24. import optparse
  25.  
  26. sys.path.insert(0, '/usr/lib/ubiquity')
  27.  
  28. from ubiquity import misc
  29.  
  30. VERSION = '1.4.11'
  31. TARGET = '/target'
  32. LOCKFILE = '/var/lock/ubiquity'
  33. lock = None
  34.  
  35. def install(frontend=None):
  36.     '''install(frontend=None) -> none
  37.     
  38.     Get the type of frontend to use and load the module for that.
  39.     If frontend is None, defaults to the first of gtkui and kde-ui that
  40.     exists.
  41.     '''
  42.     if frontend is None:
  43.         frontends = ['gtkui', 'kde-ui']
  44.     else:
  45.         frontends = [frontend]
  46.     mod = __import__('ubiquity.frontend', globals(), locals(), frontends)
  47.     for f in frontends:
  48.         if hasattr(mod, f):
  49.             ui = getattr(mod, f)
  50.             break
  51.     else:
  52.         raise AttributeError, ('No frontend available; tried %s' %
  53.                                ', '.join(frontends))
  54.  
  55.     unmount_target()
  56.     distro = misc.distribution().lower()
  57.     wizard = ui.Wizard(distro)
  58.     ret = wizard.run()
  59.     copy_debconf()
  60.     unmount_target()
  61.     if ret == 10:
  62.         wizard.do_reboot()
  63.  
  64. def copy_debconf():
  65.     """Copy a few important questions into the installed system."""
  66.     targetdb = '/target/var/cache/debconf/config.dat'
  67.     for q in ('^console-setup/',):
  68.         misc.ex('debconf-copydb', 'configdb', 'targetdb', '-p', q,
  69.                 '--config=Name:targetdb', '--config=Driver:File',
  70.                 '--config=Filename:%s' % targetdb)
  71.  
  72. def unmount_target():
  73.     paths = []
  74.     mounts = open('/proc/mounts')
  75.     for line in mounts:
  76.         path = line.split(' ')[1]
  77.         if path == '/target' or path.startswith('/target/'):
  78.             paths.append(path)
  79.     mounts.close()
  80.     paths.sort()
  81.     paths.reverse()
  82.     for path in paths:
  83.         misc.ex('umount', path)
  84.  
  85. def prepend_path(directory):
  86.     if 'PATH' in os.environ and os.environ['PATH'] != '':
  87.         os.environ['PATH'] = '%s:%s' % (directory, os.environ['PATH'])
  88.     else:
  89.         os.environ['PATH'] = directory
  90.  
  91. def release_lock():
  92.     global lock
  93.     try:
  94.         os.unlink(LOCKFILE)
  95.     except OSError:
  96.         pass
  97.     if lock is not None:
  98.         lock.close()
  99.         lock = None
  100.  
  101. def acquire_lock():
  102.     global lock
  103.     lock = open(LOCKFILE, 'w')
  104.     try:
  105.         fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
  106.     except IOError, e:
  107.         if e.errno in (errno.EACCES, errno.EAGAIN, errno.EWOULDBLOCK):
  108.             print "Ubiquity is already running!"
  109.             sys.exit(1)
  110.         raise
  111.     atexit.register(release_lock)
  112.     fcntl.fcntl(lock, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  113.     print >>lock, os.getpid()
  114.     lock.flush()
  115.     os.fsync(lock.fileno())
  116.  
  117. def main():
  118.     usage = '%prog [options] [frontend]'
  119.     parser = optparse.OptionParser(usage=usage, version=VERSION)
  120.     parser.set_defaults(
  121.         debug=('UBIQUITY_DEBUG' in os.environ),
  122.         cdebconf=False,
  123.         new_partitioner=True,
  124.         migration_assistant=True)
  125.     parser.add_option('-d', '--debug', dest='debug', action='store_true',
  126.                       help='debug mode (warning: passwords will be logged!)')
  127.     parser.add_option('--cdebconf', dest='cdebconf', action='store_true',
  128.                       help='use cdebconf instead of debconf (experimental)')
  129.     parser.add_option('--old-partitioner', dest='new_partitioner',
  130.                       action='store_false',
  131.                       help='force use of old advanced partitioner')
  132.     parser.add_option('--new-partitioner', dest='new_partitioner',
  133.                       action='store_true',
  134.                       help='use new advanced partitioner (experimental)')
  135.     parser.add_option('--no-migration-assistant', dest='migration_assistant',
  136.                       action='store_false',
  137.                       help='disable Migration Assistant')
  138.     (options, args) = parser.parse_args()
  139.  
  140.     if options.debug:
  141.         os.environ['UBIQUITY_DEBUG'] = '1'
  142.  
  143.     if options.cdebconf:
  144.         # Note that this needs to be set before DebconfCommunicate is
  145.         # imported by anything.
  146.         os.environ['DEBCONF_USE_CDEBCONF'] = '1'
  147.         prepend_path('/usr/lib/cdebconf')
  148.     prepend_path('/usr/lib/ubiquity/compat')
  149.  
  150.     if options.new_partitioner:
  151.         os.environ['UBIQUITY_NEW_PARTITIONER'] = '1'
  152.     else:
  153.         try:
  154.             del os.environ['UBIQUITY_NEW_PARTITIONER']
  155.         except KeyError:
  156.             pass
  157.  
  158.     if options.migration_assistant:
  159.         os.environ['UBIQUITY_MIGRATION_ASSISTANT'] = '1'
  160.  
  161.     if 'UBIQUITY_NEW_PARTITIONER' not in os.environ:
  162.         try:
  163.             del os.environ['UBIQUITY_MIGRATION_ASSISTANT']
  164.         except KeyError:
  165.             pass
  166.  
  167.     acquire_lock()
  168.  
  169.     if not os.path.exists('/var/log/installer'):
  170.         os.makedirs('/var/log/installer')
  171.     syslog.openlog('ubiquity', syslog.LOG_NOWAIT | syslog.LOG_PID)
  172.  
  173.     syslog.syslog("Ubiquity %s" % VERSION)
  174.     version_file = open('/var/log/installer/version', 'w')
  175.     print >>version_file, 'ubiquity %s' % VERSION
  176.     version_file.close()
  177.  
  178.     if 'UBIQUITY_DEBUG' in os.environ:
  179.         if 'UBIQUITY_DEBUG_CORE' not in os.environ:
  180.             os.environ['UBIQUITY_DEBUG_CORE'] = '1'
  181.         if 'DEBCONF_DEBUG' not in os.environ:
  182.             os.environ['DEBCONF_DEBUG'] = 'developer|filter'
  183.         # The frontend should take care of displaying a helpful message if
  184.         # we are being run without root privileges.
  185.         try:
  186.             sys.stderr = open('/var/log/installer/debug', 'a', 1)
  187.             os.dup2(sys.stderr.fileno(), 2)
  188.             print >>sys.stderr, "Ubiquity %s" % VERSION
  189.         except IOError, err:
  190.             if err.errno != errno.EACCES:
  191.                 raise
  192.  
  193.     # Default to enabling internal (non-debconf) debugging.
  194.     if 'UBIQUITY_DEBUG_CORE' not in os.environ:
  195.         os.environ['UBIQUITY_DEBUG_CORE'] = '1'
  196.  
  197.     # Clean up old state.
  198.     if os.path.exists("/var/lib/ubiquity/apt-installed"):
  199.         os.unlink("/var/lib/ubiquity/apt-installed")
  200.     if os.path.exists("/var/lib/ubiquity/remove-kernels"):
  201.         os.unlink("/var/lib/ubiquity/remove-kernels")
  202.     shutil.rmtree("/var/lib/partman", ignore_errors=True)
  203.  
  204.     if args:
  205.         install(args[0])
  206.     else:
  207.         install()
  208.  
  209. if __name__ == '__main__':
  210.     main()
  211.  
  212. # vim:ai:et:sts=4:tw=80:sw=4:
  213.